﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace BToolkit.ExeUtils
{

    [RequireComponent(typeof(BButton))]
    [RequireComponent(typeof(DragUGUI))]
    [RequireComponent(typeof(TouchRotateUIByTwoFingers))]
    public class DecalCtrl : MonoBehaviour
    {
        public BButton btnScale, btnRotate, btnDelete;
        public Transform top, bottom, left, right, anchor;

        public RectTransform rectTransform { get { return transform as RectTransform; } }
        Action<DecalCtrl> onDecalSelect;
        bool hadDownOnDecal, isScaleMode;
        float originDis;
        Vector3 originScale;
        Canvas _canvas;
        Canvas canvas { get { return _canvas ?? (_canvas = GetComponentInParent<Canvas>()); } }

        private void OnDestroy()
        {
            onDecalSelect -= OnDecalSelect;
        }

        private void Awake()
        {
            onDecalSelect += OnDecalSelect;
            GetComponent<BButton>().AddDown((int index) =>
            {
                hadDownOnDecal = true;
                onDecalSelect?.Invoke(this);
            });
            btnScale.AddDown((int index) =>
            {
                hadDownOnDecal = true;
                isScaleMode = true;
                Vector2 center = BUtils.LocalToRootPos(rectTransform);
                Vector2 barPos = BUtils.ScreenToUGUIPoint(Input.mousePosition, canvas);
                originDis = Vector2.Distance(center, barPos);
                originScale = transform.localScale;
            });
            btnRotate.AddDown((int index) =>
            {
                hadDownOnDecal = true;
            });
            if (btnDelete)
            {
                btnDelete.AddDown((int index) =>
                {
                    hadDownOnDecal = true;
                });
                btnDelete.AddClick(() =>
                {
                    Destroy(gameObject);
                });
            }
            ShowCtrl(false);
        }

        public void SetEditEnable(bool b)
        {
            if (!b) ShowCtrl(false);
            GetComponent<BButton>().enabled = b;
            GetComponent<DragUGUI>().enabled = b;
            GetComponent<TouchRotateUIByTwoFingers>().enabled = b;
        }

        void OnDecalSelect(DecalCtrl decal)
        {
            ShowCtrl(decal == this);
        }

        public void SetTexture(Texture texture)
        {
            GetComponent<RawImage>().texture = texture;
        }

        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                if (hadDownOnDecal) return;
                onDecalSelect?.Invoke(null);
            }
            if (isScaleMode)
            {
                Vector2 center = BUtils.LocalToRootPos(rectTransform);
                Vector2 barPos = BUtils.ScreenToUGUIPoint(Input.mousePosition, canvas);
                float dis = Vector2.Distance(center, barPos);
                transform.localScale = originScale * dis / originDis;
            }
            if (Input.GetMouseButtonUp(0))
            {
                hadDownOnDecal = false;
                isScaleMode = false;
            }
        }

        void ShowCtrl(bool b)
        {
            if (btnScale) btnScale.gameObject.SetActive(b);
            if (btnRotate) btnRotate.gameObject.SetActive(b);
            if (btnDelete) btnDelete.gameObject.SetActive(b);
            top.gameObject.SetActive(b);
            bottom.gameObject.SetActive(b);
            left.gameObject.SetActive(b);
            right.gameObject.SetActive(b);
            if (anchor) anchor.gameObject.SetActive(b);
        }
    }
}